package com.example.tliaswebmanagement.controller;

import com.example.tliaswebmanagement.pojo.Result;
import com.example.tliaswebmanagement.utils.AliyunOSSOperator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Slf4j
@RestController
public class UploadController {
    private final AliyunOSSOperator aliyunOSSOperator;

    public UploadController(AliyunOSSOperator aliyunOSSOperator) {
        this.aliyunOSSOperator = aliyunOSSOperator;
    }

    /**
     * 阿里云 OSS 上传
     */
    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws Exception {
        // 获取图片原始名称
        log.info("上传文件名称：{}", file.getOriginalFilename());
        // 将文件交给 OSS 储存
        String url = aliyunOSSOperator.upload(file.getBytes(), file.getOriginalFilename());
        log.info("文件在OSS中的访问路径：{}", url);

        return Result.success(url);
    }

}
